//
//  UploadPicView.m
//  TongLeLe
//
//  Created by jianglinfeng on 16/4/13.
//  Copyright © 2016年 XDD. All rights reserved.
//

#import "UploadPicView.h"
//#import "interface.h"

@interface UploadPicView()

@property (nonatomic, strong) UILabel *numLabel;

@end

@implementation UploadPicView

- (void)dealloc {

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"nextPic" object:nil];
}

- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        
        [self initializeView];
    }
    return self;
}

- (void)initializeView {

    self.backgroundColor = [self colorWithHexString:@"222222" withAlpha:0.75];
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 100)];
    bottomView.center = CGPointMake(self.center.x, self.center.y);
    bottomView.backgroundColor = [self colorWithHexString:@"333333" withAlpha:1];
    bottomView.layer.cornerRadius = 6.0;
    [self addSubview:bottomView];
    
    self.numLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 150, 20)];
    self.numLabel.center = CGPointMake(75, 50);
    self.numLabel.textAlignment = NSTextAlignmentCenter;
    self.numLabel.text = @"正在上传...";
    self.numLabel.textColor = [UIColor whiteColor];
    self.numLabel.font = [UIFont systemFontOfSize:16];
    [bottomView addSubview:self.numLabel];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatePic:) name:@"nextPic" object:nil];
}

- (void)updatePic:(NSNotification *)notification {

    NSDictionary *dic = [notification userInfo];
    NSString *str = [dic objectForKey:@"index"];
    self.numLabel.text = str;
}

- (UIColor *) colorWithHexString: (NSString *)color withAlpha:(double)alpha {
    
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [cString substringWithRange:range];
    
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:alpha];
}


@end
